//
//  PKLoginViewController.m
//  PianKe
//
//  Created by lanou on 15/10/14.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//

#import "PKLoginViewController.h"
#import "PKRootLeftViewHeaderView.h"
#import "PKRequestManager.h"
@interface PKLoginViewController ()
@property(nonatomic,strong)PKRootLeftViewHeaderView *rootLeftVC;
@end

@implementation PKLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)loginButtonAction:(id)sender {
    NSString *username = _userNameTF.text;
    NSString *password = _passwordTF.text;
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"email"]= username;
    parDic[@"passwd"]= password;
    
    
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/user/login" parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if ([dic[@"result"] integerValue] == 0) {
            NSLog(@"邮箱或者密码错误，请重试");
        }else{
            
            NSLog(@"登录成功");
            NSString *auth =dic[@"data"][@"auth"];
            NSString *uname = dic[@"uname"][@"uname"];
            [[NSUserDefaults standardUserDefaults] setValue:auth forKey:@"auth"];
            [[NSUserDefaults standardUserDefaults] setValue:uname forKey:@"uname"];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"Login" object:nil];
                   
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
        
        
        
        
    } error:^(NSError *error) {
        NSLog(@"");
    }];
    
    
    
}

- (IBAction)backButtonAction:(id)sender {
     [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)registerButtonAction:(id)sender {
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
